package cn.sibu.disalllowtouchintercept;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MyWebViewActivity extends AppCompatActivity {

    private WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_web_view);
        webView = (WebView) findViewById(R.id.my_webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setLoadsImagesAutomatically(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setAllowFileAccess(true);
        webView.setWebChromeClient(new MyWebViewChromeClient());
        webView.setWebViewClient(new MyClient());
        webView.loadUrl("http://kuaigou.sibu.cn/user/index?buyer_bid=397098&token=142C3DDDDFE6BC6338362843A65458C2&type=android");
    }


    private class MyWebViewChromeClient extends WebChromeClient{

        private ValueCallback<Uri> mUploadMessage;
        // For Android 3.0+
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
            if (mUploadMessage != null)
                return;
            mUploadMessage = uploadMsg;
            Toast.makeText(MyWebViewActivity.this,"upload",Toast.LENGTH_SHORT).show();
            selectImage();
        }
        // For Android < 3.0
        public void openFileChooser(ValueCallback<Uri> uploadMsg) {
            //openFileChooser( uploadMsg, "" );
            if (mUploadMessage != null)
                return;
            mUploadMessage = uploadMsg;
            Toast.makeText(MyWebViewActivity.this,"upload",Toast.LENGTH_SHORT).show();
            selectImage();
        }
        // For Android  > 4.1.1
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
            //openFileChooser(uploadMsg, acceptType);
            if (mUploadMessage != null)
                return;
            mUploadMessage = uploadMsg;
            Toast.makeText(MyWebViewActivity.this,"upload",Toast.LENGTH_SHORT).show();
            selectImage();
        }

        public void selectImage(){
            final ComfigDialog comfigDialog = new ComfigDialog(MyWebViewActivity.this);
            comfigDialog.myDialog("拍照", "从相册获取图片");
            comfigDialog.setOnItemClickListener(new ComfigDialog.OnItemClickListener() {
                @Override
                public void onItemClick(View v, int position) {//点击从相机拍照
                    if (position == 0) {
                        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                        comfigDialog.dismiss();
                    }
                    if (position == 1) {//点击从相册获取图片
                        Intent intent = new Intent(Intent.ACTION_PICK);
                        intent.setType("image/*");
                        comfigDialog.dismiss();
                    }
                    if (position == 2) {
                        comfigDialog.dismiss();
                    }
                }
            });

        }

    }
    private class MyClient extends WebViewClient{

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }
    }
}
